﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CarsRegistrationMobile.Models.JSON;
using CarsRegistrationMobile.Managers;

namespace CarsRegistrationMobile.Views
{
    public partial class Registration : PhoneApplicationPage
    {
        #region State

        private string menuPath;
        private IDictionary<FrameworkElement, Control> blockToBox;

        #endregion State

        public Registration()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.menuPath = "/Views/Menu.xaml";

            this.blockToBox = new Dictionary<FrameworkElement, Control>();
            this.blockToBox.Add(this.TextBlockLogin, this.TextBoxLogin);
            this.blockToBox.Add(this.TextBlockPassword, this.TextBoxPassword);
            this.blockToBox.Add(this.TextBlockName, this.TextBoxName);
            this.blockToBox.Add(this.TextBlockLastName, this.TextBoxLastName);
            this.blockToBox.Add(this.TextBlockMiddleName, this.TextBoxMiddleName);
            this.blockToBox.Add(this.TextBlockPassportSeries, this.TextBoxPassportSeries);
            this.blockToBox.Add(this.TextBlockPassportNumber, this.TextBoxPassportNumber);
        }

        private void ButtonRegistration_Click(object sender, RoutedEventArgs e)
        {
            var storageManager = ((App)Application.Current).StorageManager;

            string passHash = storageManager.MD5Encode(this.TextBoxPassword.Password);
            GotResultFromServer gotRegistrationResult = (jsonResult) =>
            {
                if (jsonResult == null)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show("Ошибка");
                    });
                    return;
                }

                if (jsonResult.ResultCode == 0)
                {
                    Dispatcher.BeginInvoke(() =>
                        {
                            storageManager.SaveSettings(new KeyValuePair<string, object>("Login", this.TextBoxLogin.Text));
                            storageManager.SaveSettings(new KeyValuePair<string, object>("PassHash", passHash));

                            Uri uri = new Uri(menuPath, UriKind.Relative);
                            NavigationService.Navigate(uri);
                        });
                }
                else
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(jsonResult.Result);
                    });
                }
            };

            ((App)Application.Current).NetworkManager.BeginRegistration(
                this.TextBoxLogin.Text, passHash, this.TextBoxName.Text,
                this.TextBoxLastName.Text, this.TextBoxMiddleName.Text,
                this.TextBoxPassportSeries.Text, this.TextBoxPassportNumber.Text,
                gotRegistrationResult);
        }

        private void TextBlock_Tap(object sender, GestureEventArgs e)
        {
            this.blockToBox[(FrameworkElement)sender].Focus();
        }
    }
}